package com.cf.r1043_d;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class d {

    // 按位统计 0..n 的位和
    static long sumDigitsUpTo(long n) {
        if (n < 0) return 0L;
        long res = 0L;
        long p = 1L;
        while (p <= n) {
            long high = n / (p * 10);
            long cur = (n / p) % 10;
            long low = n % p;

            res += high * 45L * p;
            res += cur * (low + 1);
            res += (cur * (cur - 1) / 2) * p;

            if (p > Long.MAX_VALUE / 10) break;
            p *= 10;
        }
        return res;
    }

    // 计算 1..m 拼接后的总位数
    static long totalDigitsUpTo(long m) {
        if (m <= 0) return 0L;
        long digits = 0L;
        long start = 1L;
        int len = 1;
        while (start <= m) {
            long cnt = 9L * start;
            long take = Math.min(cnt, m - start + 1);
            if (take <= 0) break;
            digits += take * len;
            if (start > Long.MAX_VALUE / 10) break;
            start *= 10;
            len++;
        }
        return digits;
    }

    /**
     * 给定 k（前 k 位），返回这些位上的数字之和
     *
     * 思路：
     *  1) 二分找到最大的 m 满足 totalDigitsUpTo(m) <= k
     *  2) 答案 = sumDigitsUpTo(m) + 前 rem 个字符来自 (m+1) 的高位字符之和
     */
    public static long solveForK(long k) {
        // binary search for max m with totalDigitsUpTo(m) <= k
        long lo = 0, hi = (long)1e15; // hi 大到足够
        while (lo < hi) {
            long mid = lo + (hi - lo + 1) / 2;
            if (totalDigitsUpTo(mid) <= k) lo = mid;
            else hi = mid - 1;
        }
        long m = lo;
        long used = totalDigitsUpTo(m);
        long ans = 0L;
        if (m > 0) ans += sumDigitsUpTo(m);
        long rem = k - used;
        if (rem > 0) {
            long next = m + 1;
            String sNext = Long.toString(next);
            for (int i = 0; i < rem && i < sNext.length(); i++) ans += sNext.charAt(i) - '0';
        }
        return ans;
    }

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);
        int T = Integer.parseInt(br.readLine().trim());
        while (T-- > 0) {
            long k = Long.parseLong(br.readLine().trim());
            out.println(solveForK(k));
        }
        out.flush();
        out.close();
        br.close();
    }
}
